package com.tsd.system.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.common.controller.BaseController;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.system.entity.SysRoleDataAccessRule;
import com.tsd.system.entity.SysRoleDataAccessRuleExt;
import com.tsd.system.entity.SysUser;
import com.tsd.system.service.SysRoleDataAccessRuleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：SysRoleDataAccessRule角色拥有的数据访问规则 控制层
 *
 * @author david
 * @date 2021/07/08 15:49:18
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "sys_role_data_access_rule-角色拥有的数据访问规则", description = "角色拥有的数据访问规则的API描述")
public class SysRoleDataAccessRuleController extends BaseController {

    @Resource
    private SysRoleDataAccessRuleService sysRoleDataAccessRuleService;

    @ApiOperation(value = "根据id获取角色拥有的数据访问规则", notes = "根据id获取角色拥有的数据访问规则")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色拥有的数据访问规则id", required = true, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getSysRoleDataAccessRuleById.action", method = RequestMethod.POST)
    public HttpResult<SysRoleDataAccessRule> getSysRoleDataAccessRuleById() {
        HttpResult<SysRoleDataAccessRule> hr = new HttpResult<SysRoleDataAccessRule>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            SysRoleDataAccessRule sysRoleDataAccessRule = sysRoleDataAccessRuleService.getSysRoleDataAccessRuleById(id);
            hr.setData(sysRoleDataAccessRule);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除角色拥有的数据访问规则", notes = "根据id删除角色拥有的数据访问规则")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "角色拥有的数据访问规则id", required = true, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteSysRoleDataAccessRule.action", method = RequestMethod.POST)
    public HttpResult<String> deleteSysRoleDataAccessRule() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            sysRoleDataAccessRuleService.deleteSysRoleDataAccessRule(id, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "保存角色拥有的数据访问规则", notes = "保存角色拥有的数据访问规则，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "角色拥有的数据访问规则", required = true, paramType = "body", dataType = "SysRoleDataAccessRule")
    })
    @ResponseBody
    @RequestMapping(value = "/saveSysRoleDataAccessRule.action", method = RequestMethod.POST)
    public HttpResult<String> saveSysRoleDataAccessRule() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String json = JSON.toJSONString(map.get("dataList"));
            List<SysRoleDataAccessRule> t = JSON.parseArray(json, SysRoleDataAccessRule.class);
            sysRoleDataAccessRuleService.saveSysRoleDataAccessRuleList(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询角色拥有的数据访问规则", notes = "分页查询角色拥有的数据访问规则")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findSysRoleDataAccessRules.action", method = RequestMethod.POST)
    public HttpResult<List<SysRoleDataAccessRuleExt>> findSysRoleDataAccessRules() {
        HttpResult<List<SysRoleDataAccessRuleExt>> hr = new HttpResult<List<SysRoleDataAccessRuleExt>>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>();
            Integer dr = HlpUtils.getInteger(map, "dr", 0);
            params.put("dr", dr);
            String search_value = HlpUtils.getString(map, "search_value");
            //super.checkEmpty(search_value, "请提供参数search_value");
            if (!HlpUtils.isEmpty(search_value)) {
                params.put("search_value", "%" + search_value + "%");
            }
            PageBean page = new PageBean(map);
            List<SysRoleDataAccessRuleExt> list = sysRoleDataAccessRuleService.findSysRoleDataAccessRules(params, page);
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

}
